// pages/interface/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookList: [],
    imgUrl: "",
    keyword: '',
    historyArr: wx.getStorageSync('history') || []
  },
  getBookList() {
    wx.showLoading({
      title: '数据查询中',
      mask: true
    })
    wx.request({
      url: 'https://hmajax.itheima.net/api/books',
      data: {
        creator: 'zhangsan'
      },
      method: 'get',
      success: (res) => {
        setTimeout(() => {
          this.setData({
            bookList: res.data.data
          }, () => {
            wx.showToast({
              title: '查询成功'
            })
          })
        }, 2000);
      },
      fail: (err) => {},
      complete: () => {
        setTimeout(() => {
          wx.hideLoading()
        }, 2000);
      },
    })
  },
  modal() {
    wx.showModal({
      title: '提示',
      content: '这是一个模态弹窗',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  sheet() {
    wx.showActionSheet({
      itemList: ['A', 'B', 'C'],
      success(res) {
        console.log(res.tapIndex)
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  setStorage() {
    const token = 'h8f93hh9rh8hehwf89rh3er8h'
    const person = {
      name: '张三',
      age: 18
    }
    wx.setStorageSync('token', token)
    wx.setStorageSync('person', person)
  },
  getStorage() {
    console.log(wx.getStorageSync('token'));
    console.log(wx.getStorageSync('person'));
  },
  delStorage() {
    wx.removeStorageSync('token')
    wx.removeStorageSync('person')
  },
  claStorage() {
    wx.clearStorageSync();
  },
  setStorageAsync() {
    wx.setStorage({
      key: "dog",
      data: {
        name: '小狗',
        age: 10
      }
    })
  },
  getStorageAsync() {
    wx.getStorage({
      key: 'dog',
      success: (res) => {
        console.log(res.data);
      }
    })
  },
  delStorageAsync() {
    wx.removeStorage({
      key: 'dog'
    })
  },
  claStorageAsync() {
    wx.clearStorage();
  },
  async uploadImg() {
    // wx.chooseMedia({
    //   count: 1,
    //   mediaType: ['image'],
    //   success: (res) => {
    //     this.setData({
    //       imgUrl: res.tempFiles[0].tempFilePath
    //     })
    //   }
    // })
    // wx.chooseMedia({
    //   count: 1,
    //   mediaType: ['image']
    // }).then((res) => {
    //   this.setData({
    //     imgUrl: res.tempFiles[0].tempFilePath
    //   })
    // })
    const res = await wx.chooseMedia({
      count: 1,
      mediaType: ['image']
    })
    this.setData({
      imgUrl: res.tempFiles[0].tempFilePath
    })
  },
  doSearch() {
    if (!this.data.keyword.trim()) {
      wx.showToast({
        title: '输入内容不能为空',
        icon: 'none'
      })
    }
    const temArr = this.data.historyArr
    temArr.push(this.data.keyword)
    this.setData({
      historyArr: temArr,
      keyword: ''
    }, () => {
      wx.setStorageSync('history', this.data.historyArr)
    })
  },
  clsHistory() {
    this.setData({
        historyArr: []
      }),
      wx.removeStorageSync('history')
  },
  click111(e) {
    console.log(e.target.dataset);
  },
  click222(e) {
    console.log(e.mark);
  }
})